package com.sh.ssao.lottery.config;

import com.sh.ssao.lottery.config.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class CommonController {
    @Autowired
    Result result;
    @RequestMapping("/welcome")
    public ModelAndView welcome(){
        log.debug("***********欢迎光临**************");
        return new ModelAndView("welcome");
    }

    @RequestMapping("/zxy")
    public ModelAndView zxy(){
        log.debug("***********欢迎光临**************");
        return new ModelAndView("zxy");
    }

    /**
     * 后台登录页
     * @return
     */
    @RequestMapping(value = {"/login","/"})
    public ModelAndView login(){
        log.debug("*************登录**********");
        return new ModelAndView("/choujiang/luck_draw/list");
    }

    /**
     * 前台登录页
     * @return
     */
    @RequestMapping(value = {"/show_login"})
    public ModelAndView show_login(){
        log.debug("*************登录**********");
        return new ModelAndView("/show/login");
    }

    @RequestMapping("/login1")
    public ModelAndView login1(){
        log.debug("*************登录**********");
        return new ModelAndView("login_v1");
    }

    @RequestMapping("/reg")
    public ModelAndView reg(){
        log.debug("*************注册页面**********");
        return new ModelAndView("reg");
    }

    /**
     * 成功
     * @return
     */
    @RequestMapping("/ok")
    public Result ok(){
        try {
            log.debug("***********操作成功************");
            return result.ok(null);
        }catch (Exception e){
            log.error("{}",e);
            return result.ok(null);
        }
    }

    /**
     * 失败
     * @return
     */
    @RequestMapping("/fail")
    public Result fail(){
        try {
            log.debug("***********操作失败************");
            return result.fail(null);
        }catch (Exception e){
            log.error("{}",e);
            return result.fail(null);
        }
    }

    /**
     *
     * 登录后台
     * @param
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/user/login")
    public ModelAndView userLogin(Model model, HttpServletRequest request){
        log.debug("*************登录**********");
        HttpSession session = request.getSession();
        //获取当前用户
        try{
            return new ModelAndView("index");
        } catch (Exception e) {
            //用户名不存在
            model.addAttribute("msg","密码错误");
            return new ModelAndView("login");
        }
    }

    /**
     *
     * 登录后台
     * @param
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/user/show_login")
    public ModelAndView show_login(Model model, HttpServletRequest request){
        log.debug("*************登录**********");
        HttpSession session = request.getSession();
        //获取当前用户
        try{
            return new ModelAndView("redirect:/show/front?userId="+session.getAttribute("userId"));
        } catch (Exception e) {
            //用户名不存在
            model.addAttribute("msg","密码错误");
            return new ModelAndView("/show/login");
        }
    }

    /**
     * 未授权提示页面
     */
    @RequestMapping("/noauth")
    public String unauthorized(){
        return "/error/noauth";
    }

}
